﻿using System;
using System.Drawing;
using System.Web.Security;
using System.Web.UI;
using SouthernLand.Common;
using SouthernLand.Common.BOL.User;

namespace SouthernLand.Membership
{
    public partial class CreateUser : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            lblTitle.Text = ResourceManager.GetString("Register_SignupPlease");

            Set_ErrorMessage();


            if (Request.QueryString["Action"] == "Edit" && !Page.IsPostBack)
            {
                if (Globals.CurrentUserName == "")
                    Response.Redirect(Globals.ApplicationPath + "Default.aspx");

                PanelLogon.Visible = false;
                btDangky.ImageUrl = "~/Images/btCapNhat.gif";
                LoadProfile(Globals.CurrentUserName);
            }
            else
            {
                txtLastName.Attributes.Add("onclick", "this.value=''");
                txtFirstName.Attributes.Add("onclick", "this.value=''");
            }
        }

        private void Set_ErrorMessage()
        {
            RegEmail.ErrorMessage = ResourceManager.GetString("Register_Email_error");
            ReqPassword.ErrorMessage = ResourceManager.GetString("Register_Password_error");
            CompareValRePassword.ErrorMessage = ResourceManager.GetString("Register_ConfirmPassword_error");
        }

        protected void btDangky_Click(object sender, ImageClickEventArgs e)
        {
            if (Request.QueryString["Action"] == "Edit")
            {
                try
                {
                    MembershipUser u = System.Web.Security.Membership.GetUser(Globals.CurrentUserName);
                    u.Email = txtEmail.Text;
                    System.Web.Security.Membership.UpdateUser(u);
                    SaveProfile(Globals.CurrentUserName);
                    lblMsg.Text = ResourceManager.GetString("ModifiedSuccessfully");
                    PanelMsg.Visible = true;
                }
                catch
                {
                    lblMsg.Text = ResourceManager.GetString("Register_DuplicateEmail");
                }
            }
            else
            {
                // First get all the values
                string user = txtUserName.Text;
                string pswd = txtPassword.Text;
                string email = txtEmail.Text;
                string role = Globals.Settings.AppRoles.KhachHang;

                MembershipCreateStatus status;
                MembershipUser userObj = System.Web.Security.Membership.CreateUser(user, pswd, email, "yes", "yes", true,
                                                                                   out status);
                if (userObj != null)
                {
                    // Now add the user to the role and display message
                    Roles.AddUserToRole(userObj.UserName, role);

                    SaveProfile(user);

                    lblMsg.ForeColor = Color.Blue;
                    lblMsg.Text = "Chúc mừng bạn đã đăng ký thành công. <br><br>";
                    lblMsg.Text = lblMsg.Text + "Hãy đăng nhập để thực hiện các chức năng của website!";
                    PanelDangky.Visible = false;
                }
                else
                {
                    // These are just three values to check for in the status
                    // enumeration; there are others.
                    switch (status)
                    {
                        case MembershipCreateStatus.DuplicateEmail:
                            lblMsg.Text = ResourceManager.GetString("Register_DuplicateEmail");
                            break;
                        case MembershipCreateStatus.DuplicateUserName:
                            lblMsg.Text = ResourceManager.GetString("Register_DuplicateUserName");
                            break;
                        case MembershipCreateStatus.ProviderError:
                            lblMsg.Text = ResourceManager.GetString("Register_ProviderError");
                            break;
                        case MembershipCreateStatus.InvalidPassword:
                            lblMsg.Text = ResourceManager.GetString("Register_InvalidPassword");
                            break;
                        default:
                            lblMsg.Text = status + ".";
                            break;
                    }
                    lblMsg.ForeColor = Color.Red;
                }

                PanelMsg.Visible = true;
            }
        }

        protected void LoadProfile(string UserName)
        {
            string UserID = Common.BOL.User.User.GetUserIDByUserName(UserName);
            User obj = Common.BOL.User.User.GetUserByID(UserID);

            txtEmail.Text = obj.Email;
            txtAddess.Text = obj.Address;
            txtPhone.Text = obj.Phone;
            txtMobile.Text = obj.Mobile;
            txtFax.Text = obj.Fax;
            //ddlCountry.SelectedValue = 1;
            //ddlJobLocation.SelectedValue = 1;
            txtWebsite.Text = obj.Website;

            //jobSeeker
            txtFirstName.Text = obj.FirstName;
            txtLastName.Text = obj.LastName;

            //Employer
            txtCompanyName.Text = obj.Company;
            txtCompanyDesc.Text = obj.CompanyDescription;
        }

        public void SaveProfile(string UserName)
        {
            var obj = new User();
            obj.UserID = Common.BOL.User.User.GetUserIDByUserName(UserName);
            obj.UserName = UserName;
            obj.CountryID = 1;
            obj.ProvinceID = 1;
            obj.Email = txtEmail.Text;
            obj.Address = txtAddess.Text;
            obj.Phone = txtPhone.Text;
            obj.Mobile = txtMobile.Text;
            obj.Fax = txtFax.Text;


            //Employers
            obj.Company = txtCompanyName.Text;
            obj.CompanyDescription = txtCompanyDesc.Text;
            obj.Website = txtWebsite.Text;

            //JobSeeker
            obj.FirstName = txtFirstName.Text;
            obj.LastName = txtLastName.Text;
            obj.Gender = "Nam";
            obj.AddedBy = Globals.CurrentUserName;
            obj.AddedDate = DateTime.Now;
            obj.IsActive = true;

            if (Request.QueryString["Action"] == "Edit")
                Common.BOL.User.User.Update(obj);
            else
                Common.BOL.User.User.Insert(obj);
        }
    }
}